home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / dial < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  10.0 KB  |  297 lines

  1. #!/bin/ksh
  2. # dial: find & dial a number in address/phone num database
  3. # @(#) dial.ksh 2.5 97/02/19
  4. # 1988, 1989 john h. dubois iii (john@armory.com)
  5. # 90/02/03 last DOS version
  6. # 90/10/01 ported to XENIX
  7. # 91/10/11 Use address to look up addresses.
  8. # 92/04/27 Changed address option specifier to +
  9. # 92/06/21 Give ATH0 as a separate modem command.
  10. # 92/06/28 Only print name of dialee if a name is given.
  11. # 92/07/19 Print entire address record;
  12. #          use +e & print a separator line if -e is given;
  13. #          remove qualifier & secondary numbers from phone number line;
  14. #          split input on every newline
  15. # 94/04/23 Use .dialrc
  16. # 94/05/12 Added speed parameter.
  17. # 96/06/11 Cleaned up help.  Changed some option meanings.  Added adpP options.
  18. # 96/06/20 Removed group separator characters.  Added c option.
  19. #          Avoid using ranges with tr.
  20. # 96/11/20 Added r option.
  21. # 97/02/19 Added vV options.  Improved test option.  Added REPEAT var.
  22.  
  23. x=0        # default uuchat debugging level
  24.  
  25. # These aliases test an integer variable to determine 
  26. # if it is 0 (false) or nonzero (true).
  27. alias istrue="test 0 -ne"    # exit zero for true, nonzero for false
  28. alias isfalse="test 0 -eq"    # exit nonzero for true, zero for false
  29.  
  30. name=${0##*/}
  31. usage=\
  32. "Usage: $name [-acehnNp] [-x<n>] [-f<addrfile>] [-l<TTY>] [-s<speed>]
  33.         [-P<pagenum>] [name|number]"
  34.  
  35. typeset -i isname=0 isnumber=0
  36. unset SPEED    # Don't want to get this from environment
  37. defSpeed=1200
  38. rcFile=.dialrc
  39. doPage=false
  40. CANON1=
  41. defDelay=3
  42. remChars=".()/-"
  43. debug=false
  44. typeset -i count=1 pcount=1
  45. repeat=false
  46. check=false
  47. pause=10
  48. vol=
  49.  
  50. while getopts :achtnNex:f:l:s:pP:d:r:v:V: opt; do
  51.     case $opt in
  52.     h) print -r -- \
  53. "$name: look up a name in the address database and dial the given phone number,
  54. or translate and dial a literal phone number.  Lower case letters are are
  55. translated to digits according to telephone keypad conventions.  The following
  56. characters are considered grouping characters and are removed from the number
  57. before dialing: $remChars
  58. $usage
  59. $name uses a modem on a TTY port to dial.  The modem must understand AT
  60. commands.
  61. Options:
  62. Some of the following options can be set in a configuration file named
  63. \"$rcFile\", which may be in the invoking user's home directory or in the
  64. directory specified by the environment variable UHOME (if both exist,
  65. assignments in the former take precedence).  In this file, values are assigned
  66. to variables in shell style, e.g. \"VARNAME=value\".  To set an option that
  67. does not take a value, use \"VARNAME=1\".  Options given in the configuration
  68. files are overridden by those given on the command line.  Variable names are
  69. given in parentheses following option descriptions.
  70. -h: Print this help.
  71. -c: Convert numbers to canonical local or area-code-prefixed form.  After the
  72.     grouping characters are removed and letters are converted to digits, if the
  73.     length of the initial contiguous string of digits is 8 and the first digit
  74.     is a 1, the 1 is removed.  If the length is 10 and the first character is
  75.     not a 1 or 0, the number is prefixed with a 1.  (CANON1)
  76. -t: Look up address & phone number but don't dial it.
  77. -N: Force [name|number] to be interpreted as a literal number.
  78. -n: Force [name|number] to be interpreted as a name to look up.
  79. -e: Print the regular expression that the address file is searched using.
  80. -x<n>: Turn on general debugging output, and set the uuchat debugging level to
  81.     <n>.  <n> must be a non-negative integer.  The default is 0.
  82. -f: Search address file <addrfile> instead of the default address files.
  83. -l<TTY>: Dial using the modem on TTY line <TTY>.  <TTY> should be
  84.     a full device name, e.g. /dev/tty1a.  The variable (PHONE) for the
  85.     parameter is also searched for in the environment.  This parameter is not
  86.     optional; either -l must be given or PHONE must be set.
  87. -s<speed>: Set the TTY Baud rate to <speed> before dialing.  The default is
  88.     to set it to $defspeed Baud.  (SPEED)
  89. -v<volume>: Set the modem speaker volume.  <volume> is an integer from 0 to 8.
  90.     0 turns the speaker off completely.  1 sets the speaker to a very low
  91.     low volume (typically inaudible).  8 sets maximum volume.  Other values set
  92.     intermediate levels.  If -v is not given, the modem will use its preset
  93.     volume level.  (VOLUME)
  94. Paging options:
  95. -p: Dial a pager.  After the given number is dialed, there is a pause, and then
  96.     the value of the PAGENUM variable is dialed, followed by a '#'.  -p can
  97.     only be used if PAGENUM is set (typically, to the local phone number).  
  98. -P<pagenum>: Like -p, except that <pagenum> is dialed instead of the value of
  99.     the PAGER variable.
  100. -a: Automatically sets the -p option if a name ending in \":pager\" is given.
  101.     (AUTOPAGE)
  102. -d<page-delay>: The pause before sending the dialback number to a pager
  103.     service.  A number from 1 to 10 may be given.  Each increment of one
  104.     increases the pause by two seconds.  The default value is $defDelay.
  105.     (PAGEDELAY)
  106. -r<repeat>: Send the entire dialing sequence <repeat> times, pausing $pause
  107.     seconds after each dialing sequence is completed.  This is for sending
  108.     multiple identical pages via unreliable paging services.  (REPEAT)
  109. -V<volume>: Like -v, but only for pager calls.  Overrides -v and PAGER
  110.     variable.  (PAGERVOLUME)"
  111.        exit 0;;
  112.     a) AUTOPAGE=1;;
  113.     c) CANON1=1;;
  114.     d) PAGEDELAY=$OPTARG;;
  115.     f) files="$files $OPTARG";;    # add arg to list of address files
  116.     t) check=true;;    # search for address but don't dial
  117.     N) isnumber=1;;    # force name arg to be interpreted as number
  118.     n) isname=1;;    # force name arg to be interpreted as name
  119.     l) PHONE=$OPTARG;;    # tty port to use to dial
  120.     s) SPEED=$OPTARG;;    # speed to set tty to
  121.     x) debug=true; x=$OPTARG;;    # set uuchat debugging level
  122.     e) e=+e;;
  123.     p) doPage=true;;
  124.     P) pageNum=$OPTARG; doPage=true;;
  125.     r) pcount=$OPTARG;;
  126.     v) vol=$OPTARG;;
  127.     V) pvol=$OPTARG;;
  128.     +?) print -r -- "$name: options should not be preceded by a '+'."; exit 1;;
  129.     :) 
  130.     print -r -u2 -- \
  131.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  132.     exit 1
  133.     ;;
  134.     ?) print -r -- "$name: bad option '$OPTARG'.  Use -h for help."; exit 1;;
  135.     esac
  136. done
  137.  
  138. # remove args that were options
  139. let OPTIND=OPTIND-1
  140. shift $OPTIND
  141.  
  142. if [ $# -lt 1 ]; then
  143.     print -r -- "$name: not enough arguments.  Use -h for help."
  144.     exit
  145. fi
  146.  
  147. [ -n "$files" ] && files="-a $files"
  148.  
  149. # Save these so they won't be overridden by .dialrc
  150. ttyPort=$PHONE
  151. ttySpeed=$SPEED
  152.  
  153. # Source file in UHOME first, so that values in HOME will have precedence.
  154. rc=$UHOME/$rcFile
  155. [ -f $rc -a -r $rc ] && . $rc
  156. rc=$HOME/$rcFile
  157. [ -f $rc -a -r $rc ] && . $rc
  158.  
  159. if $debug; then
  160.     print -r -- "CANON1=$CANON1"    # add others here as needed
  161. fi
  162.  
  163. [ -z "$ttyPort" ] && ttyPort=$PHONE
  164. [ -z "$ttySpeed" ] && ttySpeed=$SPEED
  165. [ -z "$ttySpeed" ] && ttySpeed=$defSpeed
  166. [ -z "$pageNum" ] && pageNum=$PAGENUM
  167. [ -z "$PAGEDELAY" ] && PAGEDELAY=$defDelay
  168. [ -z "$pcount" ] && pcount=$REPEAT
  169. [ -z "$vol" ] && vol=$VOLUME
  170. [ -z "$pvol" ] && pvol=$PAGERVOLUME
  171. $doPage && [ -n "$pvol" ] && vol=$pvol
  172. if [ -n "$vol" ]; then
  173.     if [[ "$vol" != [0-8] ]]; then
  174.     print -u2 -- "$name: Bad volume level: $vol."
  175.     exit 1
  176.     fi
  177.     [ vol -eq 0 ] && vol=M0 || vol=M1L$((vol-1))
  178. fi
  179.  
  180. [[ -n "$AUTOPAGE" && "$*" = *:pager ]] && doPage=true
  181.  
  182. if $doPage; then
  183.     if [ -z "$pageNum" ]; then
  184.     print -u2 "$name: Cannot page without PAGENUM set."
  185.     exit 1
  186.     fi
  187.     [ -n "$pcount" ] && count=pcount
  188. fi
  189.  
  190. if [ -z "$ttyPort" ]; then
  191.     print -u2 "$name: PHONE not set."
  192.     $check || exit 1
  193. fi
  194.  
  195. if [ -n "$PAGEDELAY" -a "$PAGEDELAY" -lt 1 ]; then
  196.     print -u2 "Pager delay must an integer between 1 and 10."
  197.     exit 1
  198. fi
  199.  
  200. # Look up name if it's not specified as a literal number and
  201. # either doesn't begin with a digit or is specified as a name
  202. if isfalse isnumber && [[ $1 != [0-9]* ]] || istrue isname; then
  203.     entry="$(address $e +d@ $files $*)" || exit 1
  204.     print -r -- "$entry" | {
  205.     read Name
  206.     read Address
  207.     read Number
  208.     }
  209.     [ -n "$e" ] && print ""
  210.     if [ -z "$entry" ]; then
  211.     print -u2 "No entry for $*."
  212.     exit
  213.     fi
  214.     if [ -z "$Number" ]; then
  215.     print -u2 "Entry for $* has no phone number."
  216.     exit
  217.     fi
  218.     if [[ "$entry" = *+* ]]; then
  219.     print -u2 "Name is ambiguous:"
  220.     print -u2 "$entry"
  221.     exit
  222.     fi
  223.     print -r -- "$entry
  224. "
  225.     Number=${Number%%  *}    # Remove other number fields, if any
  226.     Number=${Number##*:}    # Remove qualifier, if any
  227.     who="$Name at "
  228. else
  229.     Number=$1
  230. fi
  231.  
  232. if $doPage; then
  233.     whatP=" and paging"
  234.     typeset -L$PAGEDELAY delay=,,,,,,,,,,
  235.     Number="$Number$delay$pageNum#"
  236. fi
  237.  
  238. # Don't translate upper case letters; they may be modem commands
  239. if [[ $Number = *[a-z]* ]]; then
  240.     orignum=$Number
  241.     # Must give explicit char list to tr instead of [a-p][r-y] to avoid
  242.     # version-dependent behaviour of tr.
  243.     Number=$(print -r -- $Number | tr abcdefghijklmnoprstuvwxy \
  244.     222333444555666777888999)
  245.     translated=true
  246. else
  247.     translated=false
  248. fi
  249.  
  250. Number=$(print -r -- "$Number" | tr -d "$remChars")
  251.  
  252. $debug && print -r -- "Number before canonicalization: $Number"
  253.  
  254. if [ -n "$CANON1" ]; then
  255.     typeset -i length
  256.     dig=${Number%%*([!0-9]*)}
  257.     length=${#dig}
  258.     if [[ length -eq 8 && "$Number" = 1* ]]; then
  259.     Number="${Number#1}"
  260.     elif [[ length -eq 10 && "$Number" != [01]* ]]; then
  261.     Number="1$Number"
  262.     fi
  263.     $debug && print -r -- "Number after canonicalization: $Number"
  264. fi
  265.  
  266. $check && act="Would dial" || act="Dialing"
  267. if $translated; then
  268.     print -r -- "$act$whatP $who$orignum ($Number) on $ttyPort."
  269. else
  270.     print -r -- "$act$whatP $who$Number on $ttyPort."
  271. fi
  272.  
  273. # A separate ATH0 works more reliably than H0 at the end of dial string.
  274.  
  275. # E1: echo commands
  276. # Q0: quiet off (response codes on)
  277. # M0: speaker off
  278. # S11: set DTMF period
  279. # ;: return to command mode after dialing
  280. # H0: hang up
  281. typeset -i i=0
  282. set -- /usr/lib/uucp/uuchat \
  283. "-x$x" "$ttyPort" "$ttySpeed" "" "ATE1Q0${vol}S11=50DT${Number};" OK ATH0 OK
  284. while [ i -lt count ]; do
  285.     $check && print -r -- "Would issue command:
  286. $*" || "$@"
  287.     let i+=1
  288.     if $doPage; then
  289.     [ count -gt 1 ] && print -r -- "Sent page #$i." ||
  290.     print -r -- "Sent page."
  291.     if [ i -lt count ]; then
  292.         print -r -- "Waiting for $pause seconds..."
  293.         sleep $pause
  294.     fi
  295.     fi
  296. done
  297.